package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    OrderSettingMapper orderSettingMapper;
    @Override
    public void setNumberByDate(OrderSetting orderSetting) {
        //我们需要对日期查询是否存在预约设置
        Integer count=orderSettingMapper.findCountByDate(orderSetting.getOrderDate());
        if(count>0){
            //存在就更新
            orderSettingMapper.edit(orderSetting);
        }else{
            orderSettingMapper.add(orderSetting);
        }

    }

    @Override
    public void add(ArrayList<OrderSetting> orderSettings) {
        //因为每台都要判断,所以不能批量插入
        for (OrderSetting orderSetting : orderSettings) {
            setNumberByDate(orderSetting);
        }
        //可以使用多线程导入数据
        //用Google的Guava里面的List
    }

    @Override
    public List<Map> findByDate(String date) {
        ArrayList<Map> result = new ArrayList<>();
        String start=date+"-01";
        String end=date+"-31";
        List<OrderSetting> orderSettings = orderSettingMapper.findByDate(start, end);
        if(CollectionUtil.isNotEmpty(orderSettings)){
            for (OrderSetting orderSetting : orderSettings) {
                Date orderDate = orderSetting.getOrderDate();
                Map map=new HashMap();
                map.put("date",orderDate.getDate());
                map.put("month",orderDate.getMonth());
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());
                result.add(map);
            }
        }
        return result;
    }
}
